package com.jyxd.web.controller.ZSqlSettings;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jyxd.web.data.ZSqlSettings.ZSqlSettingsEntity;
import com.jyxd.web.data.ZSqlSettings.ZSqlTypeEntity;
import com.jyxd.web.data.ZSqlSettings.form.ZSqlSettingSearchForm;
import com.jyxd.web.service.ZSqlSettings.ZSqlSettingsService;
import com.jyxd.web.service.ZSqlSettings.ZSqlTypeService;
import com.jyxd.web.util.HttpCode;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/zsql")
public class ZSqlSettingController {

    @Resource
    private ZSqlSettingsService zsqlSettingsService;

    @Resource
    private ZSqlTypeService zsqlTypeService;

    @PostMapping("/sqlSetting/search")
    public String getSqlSetting(@RequestBody ZSqlSettingSearchForm zSqlSettingSearchForm){
        JSONObject json = new JSONObject();
        json.put("code", HttpCode.FAILURE_CODE.getCode());
        json.put("data", new ArrayList<>());
        json.put("msg", "暂无数据");
        Page<ZSqlSettingsEntity> result = zsqlSettingsService.getSqlSetting(zSqlSettingSearchForm);
        if (result.getRecords() != null && !result.getRecords().isEmpty()) {
            json.put("msg", "查询成功");
            json.put("data", result.getRecords());
        }
        json.put("totalCount", result.getRecords().size());
        json.put("code", HttpCode.OK_CODE.getCode());
        return json.toString();
    }

    @GetMapping("/sqlType/search")
    public String getSqlType(){
        JSONObject json = new JSONObject();
        json.put("code", HttpCode.FAILURE_CODE.getCode());
        json.put("data", new ArrayList<>());
        json.put("msg", "暂无数据");
        List<ZSqlTypeEntity> list = zsqlTypeService.list();
        if (list != null && !list.isEmpty()) {
            json.put("msg", "查询成功");
            json.put("data", list);
        }
        json.put("code", HttpCode.OK_CODE.getCode());
        return json.toString();
    }

    @PostMapping("/sqlSetting/add")
    public String addSqlSetting(@RequestBody ZSqlSettingsEntity zSqlSettingsEntity){
        JSONObject json = new JSONObject();
        json.put("code", HttpCode.FAILURE_CODE.getCode());
        json.put("data", new ArrayList<>());
        json.put("msg", "暂无数据");
        zsqlSettingsService.save(zSqlSettingsEntity);
        json.put("code", HttpCode.OK_CODE.getCode());
        json.put("msg", "新增成功");
        json.put("data", zSqlSettingsEntity);
        return json.toString();
    }

    @PostMapping("/sqlSetting/update")
    public String updateSqlSetting(@RequestBody ZSqlSettingsEntity zSqlSettingsEntity){
        JSONObject json = new JSONObject();
        json.put("code", HttpCode.FAILURE_CODE.getCode());
        json.put("data", new ArrayList<>());
        json.put("msg", "暂无数据");
        zsqlSettingsService.updateById(zSqlSettingsEntity);
        json.put("code", HttpCode.OK_CODE.getCode());
        json.put("msg", "修改成功");
        json.put("data", zSqlSettingsEntity);
        return json.toString();
    }

    @DeleteMapping("/sqlSetting/delete/{id}")
    public String deleteSqlSetting(@PathVariable("id") Long id){
        JSONObject json = new JSONObject();
        json.put("code", HttpCode.FAILURE_CODE.getCode());
        json.put("data", new ArrayList<>());
        json.put("msg", "暂无数据");
        zsqlSettingsService.removeById(id);
        json.put("msg", "删除成功");
        json.put("code", HttpCode.OK_CODE.getCode());
        return json.toString();
    }

    @PostMapping("/sqlType/add")
    public String addSqlType(@RequestBody ZSqlTypeEntity zSqlTypesEntity){
        JSONObject json = new JSONObject();
        json.put("code", HttpCode.FAILURE_CODE.getCode());
        json.put("data", new ArrayList<>());
        json.put("msg", "暂无数据");
        zsqlTypeService.save(zSqlTypesEntity);
        json.put("code", HttpCode.OK_CODE.getCode());
        json.put("msg", "新增成功");
        json.put("data", zSqlTypesEntity);
        return json.toString();
    }

    @PostMapping("/sqlType/update")
    public String updateSqlType(@RequestBody ZSqlTypeEntity zSqlTypesEntity){
        JSONObject json = new JSONObject();
        json.put("code", HttpCode.FAILURE_CODE.getCode());
        json.put("data", new ArrayList<>());
        json.put("msg", "暂无数据");
        zsqlTypeService.updateById(zSqlTypesEntity);
        json.put("code", HttpCode.OK_CODE.getCode());
        json.put("msg", "修改成功");
        json.put("data", zSqlTypesEntity);
        return json.toString();
    }

    @DeleteMapping("/sqlType/delete/{id}")
    public String deleteSqlType(@PathVariable("id") Long id){
        JSONObject json = new JSONObject();
        json.put("code", HttpCode.FAILURE_CODE.getCode());
        json.put("data", new ArrayList<>());
        json.put("msg", "暂无数据");
        zsqlTypeService.removeById(id);
        json.put("msg", "删除成功");
        json.put("code", HttpCode.OK_CODE.getCode());
        return json.toString();
    }
}
